package com.jy.api.domain;

import com.jy.api.signUpReq.stock.BarCheckReq;
import com.jy.api.signUpReq.stock.InventoryCheckReq;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 库存盘点主记录
 * <p>
 * Created by lihu on 2018/2/2.
 */
@Entity
@Data
@NoArgsConstructor
@Table(uniqueConstraints = @UniqueConstraint(name = "UK_STOCK_CHECK_ORDER", columnNames = {"code"}))
public class StockCheck {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    /**
     * 唯一约束
     */
    private String code;

    @Column(length = 50)
    private String storeId;

    @Enumerated(EnumType.STRING)
    private StockLevel stockLevel;

    /**
     * 操作人id
     */
    private String operatorId;

    /**
     * 操作人名称
     */
    private String operatorName;

    /**
     * 种类
     */
    private int skuCount;

    /**
     * 差异数量
     * 大于0表示盘盈，小于0表示盘亏
     */
    private int quantity = 0;
    /**
     * 备注
     */
    private String notes;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "addTime")
    @JoinColumn(name = "check_id")
    private List<StockCheckTransaction> items = new ArrayList<>();

    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;


    public StockCheck(InventoryCheckReq req, StockLevel stockLevel) {
        this.code = req.getCheckCode();
        this.storeId = req.getStoreId();
        this.stockLevel = stockLevel;
        this.operatorId = req.getOperatorId();
        this.operatorName = req.getOperatorName();
        this.skuCount = req.getKind();
        this.notes = req.getNotes();
    }

    public StockCheck(BarCheckReq req, StockLevel stockLevel) {
        this.code = req.getCheckCode();
        this.storeId = req.getStoreId();
        this.stockLevel = stockLevel;
        this.operatorId = req.getOperatorId();
        this.operatorName = req.getOperatorName();
        this.skuCount = req.getKind();
        this.notes = req.getNotes();
    }
}
